var util = require('../../utils/util.js');
const api = require('../../config/api.js');
import WxValidate from '../../lib/wx-validate/WxValidate.js'
//获取应用实例
const app = getApp();
var that;
Page({
  data: {
    form: {
      phone: '',
      scene:'',
      verifiCode: '',
      captchaKey: '',
    },
    btnTxt: '获取验证码',
    isSendVerifiCode: false,
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    that = this;
    that.initValidate();
    if (options.serial){
      that.setData({
        'form.scene': options.serial
      });
    }else{
      util.confirm.modalOk('绑定序列号异常!');
    }
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  sendCode: function () {
    if (that.data.isSendVerifiCode) return true;
    if (!util.isValidPhone(that.data.form.phone)) {
      util.confirm.modal('请填写正确的手机号！');
    } else {
      var tmpTxt = that.data.btnTxt;
      app.postForm(api.QrSendVerifiCode, { phone: that.data.form.phone, isAllow: true }).then(rsp => {
        let captchaKey = rsp.captchaKey;
        that.setData({
          btnTxt: '已发送......',
          isSendVerifiCode: true,
          'form.captchaKey': captchaKey
        });
        var time = 60;
        var timer = setInterval(function () {
          time--;
          that.setData({
            btnTxt: time + 's'
          });
          if (time == 0) {
            time = 60;
            that.setData({
              btnTxt: tmpTxt,
              isSendVerifiCode: false
            });
            clearInterval(timer);
          }
        }, 1000);
      }).catch((err) => {
        console.log('err', err);
      });
    }
  },
  submitForm(e) {
    const params = e.detail.value
    //console.log(params)
    // 传入表单数据，调用验证方法
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      util.confirm.modal(error.msg);
      return false
    }
    if (util.isNullOrEmpty(that.data.form.scene)){
      util.toast.pure('绑定标识异常!');
      return false;
    }
    let p = that.data.form;
    app.postForm(api.QrBind, p, { isAllow: true}).then(rsp => {
      util.confirm.modalOk('绑定成功!', function(){
        wx.redirectTo({
          url: '/pages/houseRes/share/index?id=' + rsp.data.homeownersNo,
        })
      });
    }).catch((err) => {
      console.log('err', err);
    });
  },
  bindInput: function (e) {
    let name = e.currentTarget.dataset.name;
    let changeVal = {};
    changeVal[name] = e.detail.value;
    that.setData(changeVal);
  },
  clearInput: function (e) {
    let name = e.currentTarget.id;
    let changeVal = {};
    changeVal[name] = '';
    that.setData(changeVal);
  },
  initValidate() {
    // 验证字段的规则
    const rules = {
      phone: {
        required: true,
        tel: true,
      },
      verifiCode: {
        required: true,
        number: true,
        minlength: 6,
        maxlength: 6,
      },
    }

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      phone: {
        required: '请输入手机号',
        tel: '手机号格式错误'
      },
      verifiCode: {
        required: '请输入验证码',
        number: '验证码为6为数字',
        minlength: '验证码为6为数字',
        maxlength: '验证码为6为数字',
      },
    }
    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages)
  },
  onPullDownRefresh(){
    wx.stopPullDownRefresh();
  },
})